{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:25:37.514726Z",
     "start_time": "2018-08-22T09:25:34.871512Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:26:20.653028Z",
     "start_time": "2018-08-22T09:25:37.516609Z"
    }
   },
   "outputs": [],
   "source": [
    "train = pd.read_csv('../input/application_train.csv.zip')\n",
    "test  = pd.read_csv('../input/application_test.csv.zip')\n",
    "\n",
    "trte = pd.concat([train, test], ignore_index=True)\n",
    "\n",
    "prev = pd.read_csv('../input/previous_application.csv.zip')\n",
    "prev = pd.merge(prev, trte[['SK_ID_CURR', 'CODE_GENDER']], on='SK_ID_CURR')\n",
    "\n",
    "\n",
    "keys = ['AMT_ANNUITY',\n",
    "        'AMT_CREDIT',\n",
    "        'AMT_GOODS_PRICE',\n",
    "        'NAME_CONTRACT_TYPE',\n",
    "        'WEEKDAY_APPR_PROCESS_START',\n",
    "        'CODE_GENDER',\n",
    "        'HOUR_APPR_PROCESS_START',\n",
    "        'NAME_TYPE_SUITE']\n",
    "\n",
    "prev_test = pd.merge(prev, test.drop_duplicates(keys, False), \n",
    "                     on=keys, how='inner')\n",
    "\n",
    "# current best\n",
    "sub = pd.read_csv('../data/stochastic_blending_v3-3_0.411rk7_0.304mw1_0.285t1_0.81031CV_0.3Adv.csv')\n",
    "\n",
    "pos = pd.read_csv('../input/POS_CASH_balance.csv.zip')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# If DPD mean == 0, TARGET = 0?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:05.482809Z",
     "start_time": "2018-08-22T09:27:05.475575Z"
    }
   },
   "outputs": [],
   "source": [
    "leak_ids = prev_test.SK_ID_CURR_y.value_counts()\n",
    "leak_ids_test = leak_ids[leak_ids==1].index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:10.900225Z",
     "start_time": "2018-08-22T09:27:10.888735Z"
    }
   },
   "outputs": [],
   "source": [
    "df = prev_test[prev_test.SK_ID_CURR_y.isin(leak_ids_test)][['SK_ID_PREV', 'SK_ID_CURR_x', 'SK_ID_CURR_y']].sort_values('SK_ID_PREV')\n",
    "leak_ids_prev = df.SK_ID_PREV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:19.521727Z",
     "start_time": "2018-08-22T09:27:12.173484Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((10397, 8), (2072, 8))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos[pos.SK_ID_CURR.isin(leak_ids_test)].shape, pos[pos.SK_ID_PREV.isin(leak_ids_prev)].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:23.257074Z",
     "start_time": "2018-08-22T09:27:19.523311Z"
    }
   },
   "outputs": [],
   "source": [
    "pos_ = pos[pos.SK_ID_PREV.isin(leak_ids_prev)]\n",
    "\n",
    "dpd_mean = pos_.groupby('SK_ID_PREV').SK_DPD.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:40.783849Z",
     "start_time": "2018-08-22T09:27:40.780097Z"
    }
   },
   "outputs": [],
   "source": [
    "df.set_index('SK_ID_PREV', inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:44.123108Z",
     "start_time": "2018-08-22T09:27:44.104129Z"
    }
   },
   "outputs": [],
   "source": [
    "df['dpd_mean'] = dpd_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:45.368188Z",
     "start_time": "2018-08-22T09:27:45.340596Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    161.000000\n",
       "mean       0.063685\n",
       "std        0.334841\n",
       "min        0.000000\n",
       "25%        0.000000\n",
       "50%        0.000000\n",
       "75%        0.000000\n",
       "max        3.380952\n",
       "Name: dpd_mean, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dpd_mean.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:59.032664Z",
     "start_time": "2018-08-22T09:27:59.027660Z"
    }
   },
   "outputs": [],
   "source": [
    "ids_0 = df[df.dpd_mean==0].SK_ID_CURR_y.tolist()\n",
    "ids_1 = df[df.dpd_mean>0].SK_ID_CURR_y.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:27:59.728281Z",
     "start_time": "2018-08-22T09:27:59.708112Z"
    }
   },
   "outputs": [],
   "source": [
    "sub.sort_values('TARGET', inplace=True)\n",
    "\n",
    "sub_0 = sub[sub.SK_ID_CURR.isin(ids_0)].reset_index(drop=True)\n",
    "sub_1 = sub[sub.SK_ID_CURR.isin(ids_1)].reset_index(drop=True)\n",
    "sub_other = sub[~sub.SK_ID_CURR.isin(ids_0+ids_1)].reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:08.815183Z",
     "start_time": "2018-08-22T09:28:08.799460Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SK_ID_CURR</th>\n",
       "      <th>TARGET</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102293</td>\n",
       "      <td>0.006073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>277290</td>\n",
       "      <td>0.013971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>108763</td>\n",
       "      <td>0.016002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>318857</td>\n",
       "      <td>0.018854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>324995</td>\n",
       "      <td>0.022567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>170203</td>\n",
       "      <td>0.022608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>214141</td>\n",
       "      <td>0.025275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>347097</td>\n",
       "      <td>0.037564</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>430467</td>\n",
       "      <td>0.040969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>252092</td>\n",
       "      <td>0.041708</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>268373</td>\n",
       "      <td>0.046139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>116228</td>\n",
       "      <td>0.050098</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>192289</td>\n",
       "      <td>0.050509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>240888</td>\n",
       "      <td>0.061936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>337868</td>\n",
       "      <td>0.075538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>384657</td>\n",
       "      <td>0.092893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>435099</td>\n",
       "      <td>0.097079</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>434608</td>\n",
       "      <td>0.097571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>405690</td>\n",
       "      <td>0.101448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>177820</td>\n",
       "      <td>0.106249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>401547</td>\n",
       "      <td>0.118989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>315494</td>\n",
       "      <td>0.127790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>334273</td>\n",
       "      <td>0.129677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>213935</td>\n",
       "      <td>0.131667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>409985</td>\n",
       "      <td>0.133411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>447361</td>\n",
       "      <td>0.133801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>286924</td>\n",
       "      <td>0.136755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>147742</td>\n",
       "      <td>0.143731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>166941</td>\n",
       "      <td>0.147239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>314843</td>\n",
       "      <td>0.150254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>120</th>\n",
       "      <td>112234</td>\n",
       "      <td>0.744174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>121</th>\n",
       "      <td>374420</td>\n",
       "      <td>0.747395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122</th>\n",
       "      <td>163039</td>\n",
       "      <td>0.747702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123</th>\n",
       "      <td>376488</td>\n",
       "      <td>0.750349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>124</th>\n",
       "      <td>340630</td>\n",
       "      <td>0.765961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>125</th>\n",
       "      <td>406872</td>\n",
       "      <td>0.798026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>126</th>\n",
       "      <td>410626</td>\n",
       "      <td>0.814028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>127</th>\n",
       "      <td>175682</td>\n",
       "      <td>0.815136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>128</th>\n",
       "      <td>271135</td>\n",
       "      <td>0.830194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>129</th>\n",
       "      <td>321035</td>\n",
       "      <td>0.842586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>130</th>\n",
       "      <td>214088</td>\n",
       "      <td>0.848125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>131</th>\n",
       "      <td>185121</td>\n",
       "      <td>0.851715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>260364</td>\n",
       "      <td>0.864701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133</th>\n",
       "      <td>142676</td>\n",
       "      <td>0.875759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>275954</td>\n",
       "      <td>0.879390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>278344</td>\n",
       "      <td>0.880806</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>168093</td>\n",
       "      <td>0.884560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>112414</td>\n",
       "      <td>0.888868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>218321</td>\n",
       "      <td>0.892130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>372942</td>\n",
       "      <td>0.902019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>407825</td>\n",
       "      <td>0.914307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>317809</td>\n",
       "      <td>0.919149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>443364</td>\n",
       "      <td>0.923334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>223571</td>\n",
       "      <td>0.932320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>202697</td>\n",
       "      <td>0.948814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>233172</td>\n",
       "      <td>0.949573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>317323</td>\n",
       "      <td>0.952651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>271682</td>\n",
       "      <td>0.954087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>103570</td>\n",
       "      <td>0.956959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>412638</td>\n",
       "      <td>0.960918</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     SK_ID_CURR    TARGET\n",
       "0        102293  0.006073\n",
       "1        277290  0.013971\n",
       "2        108763  0.016002\n",
       "3        318857  0.018854\n",
       "4        324995  0.022567\n",
       "5        170203  0.022608\n",
       "6        214141  0.025275\n",
       "7        347097  0.037564\n",
       "8        430467  0.040969\n",
       "9        252092  0.041708\n",
       "10       268373  0.046139\n",
       "11       116228  0.050098\n",
       "12       192289  0.050509\n",
       "13       240888  0.061936\n",
       "14       337868  0.075538\n",
       "15       384657  0.092893\n",
       "16       435099  0.097079\n",
       "17       434608  0.097571\n",
       "18       405690  0.101448\n",
       "19       177820  0.106249\n",
       "20       401547  0.118989\n",
       "21       315494  0.127790\n",
       "22       334273  0.129677\n",
       "23       213935  0.131667\n",
       "24       409985  0.133411\n",
       "25       447361  0.133801\n",
       "26       286924  0.136755\n",
       "27       147742  0.143731\n",
       "28       166941  0.147239\n",
       "29       314843  0.150254\n",
       "..          ...       ...\n",
       "120      112234  0.744174\n",
       "121      374420  0.747395\n",
       "122      163039  0.747702\n",
       "123      376488  0.750349\n",
       "124      340630  0.765961\n",
       "125      406872  0.798026\n",
       "126      410626  0.814028\n",
       "127      175682  0.815136\n",
       "128      271135  0.830194\n",
       "129      321035  0.842586\n",
       "130      214088  0.848125\n",
       "131      185121  0.851715\n",
       "132      260364  0.864701\n",
       "133      142676  0.875759\n",
       "134      275954  0.879390\n",
       "135      278344  0.880806\n",
       "136      168093  0.884560\n",
       "137      112414  0.888868\n",
       "138      218321  0.892130\n",
       "139      372942  0.902019\n",
       "140      407825  0.914307\n",
       "141      317809  0.919149\n",
       "142      443364  0.923334\n",
       "143      223571  0.932320\n",
       "144      202697  0.948814\n",
       "145      233172  0.949573\n",
       "146      317323  0.952651\n",
       "147      271682  0.954087\n",
       "148      103570  0.956959\n",
       "149      412638  0.960918\n",
       "\n",
       "[150 rows x 2 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:10.298820Z",
     "start_time": "2018-08-22T09:28:10.290450Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SK_ID_CURR</th>\n",
       "      <th>TARGET</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>367663</td>\n",
       "      <td>0.007119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>122257</td>\n",
       "      <td>0.034897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>137590</td>\n",
       "      <td>0.107029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>447230</td>\n",
       "      <td>0.184453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>193198</td>\n",
       "      <td>0.238245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>219817</td>\n",
       "      <td>0.272546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>442195</td>\n",
       "      <td>0.414923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>163895</td>\n",
       "      <td>0.615378</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>352451</td>\n",
       "      <td>0.905753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>311024</td>\n",
       "      <td>0.912912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>365575</td>\n",
       "      <td>0.985352</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    SK_ID_CURR    TARGET\n",
       "0       367663  0.007119\n",
       "1       122257  0.034897\n",
       "2       137590  0.107029\n",
       "3       447230  0.184453\n",
       "4       193198  0.238245\n",
       "5       219817  0.272546\n",
       "6       442195  0.414923\n",
       "7       163895  0.615378\n",
       "8       352451  0.905753\n",
       "9       311024  0.912912\n",
       "10      365575  0.985352"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Swap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:12.423466Z",
     "start_time": "2018-08-22T09:28:12.418912Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((150, 2), (11, 2))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_0.shape, sub_1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:14.217052Z",
     "start_time": "2018-08-22T09:28:14.208676Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    150.000000\n",
       "mean       0.432215\n",
       "std        0.282045\n",
       "min        0.006073\n",
       "25%        0.205825\n",
       "50%        0.389217\n",
       "75%        0.639494\n",
       "max        0.960918\n",
       "Name: TARGET, dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_0.TARGET.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:15.055381Z",
     "start_time": "2018-08-22T09:28:15.047574Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    11.000000\n",
       "mean      0.425328\n",
       "std       0.369244\n",
       "min       0.007119\n",
       "25%       0.145741\n",
       "50%       0.272546\n",
       "75%       0.760565\n",
       "max       0.985352\n",
       "Name: TARGET, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_1.TARGET.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:20.350925Z",
     "start_time": "2018-08-22T09:28:20.346643Z"
    }
   },
   "outputs": [],
   "source": [
    "target_all = sorted(sub_0.append(sub_1).TARGET.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:21.154836Z",
     "start_time": "2018-08-22T09:28:21.151567Z"
    }
   },
   "outputs": [],
   "source": [
    "target_0, target_1 = target_all[:sub_0.shape[0]], target_all[sub_0.shape[0]:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:24.901265Z",
     "start_time": "2018-08-22T09:28:24.897241Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(150, 11)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(target_0), len(target_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:32.567763Z",
     "start_time": "2018-08-22T09:28:32.563699Z"
    }
   },
   "outputs": [],
   "source": [
    "sub_0.TARGET = target_0\n",
    "sub_1.TARGET = target_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:32.939000Z",
     "start_time": "2018-08-22T09:28:32.922347Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SK_ID_CURR</th>\n",
       "      <th>TARGET</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102293</td>\n",
       "      <td>0.006073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>277290</td>\n",
       "      <td>0.007119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>108763</td>\n",
       "      <td>0.013971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>318857</td>\n",
       "      <td>0.016002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>324995</td>\n",
       "      <td>0.018854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>170203</td>\n",
       "      <td>0.022567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>214141</td>\n",
       "      <td>0.022608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>347097</td>\n",
       "      <td>0.025275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>430467</td>\n",
       "      <td>0.034897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>252092</td>\n",
       "      <td>0.037564</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>268373</td>\n",
       "      <td>0.040969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>116228</td>\n",
       "      <td>0.041708</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>192289</td>\n",
       "      <td>0.046139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>240888</td>\n",
       "      <td>0.050098</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>337868</td>\n",
       "      <td>0.050509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>384657</td>\n",
       "      <td>0.061936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>435099</td>\n",
       "      <td>0.075538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>434608</td>\n",
       "      <td>0.092893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>405690</td>\n",
       "      <td>0.097079</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>177820</td>\n",
       "      <td>0.097571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>401547</td>\n",
       "      <td>0.101448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>315494</td>\n",
       "      <td>0.106249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>334273</td>\n",
       "      <td>0.107029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>213935</td>\n",
       "      <td>0.118989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>409985</td>\n",
       "      <td>0.127790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>447361</td>\n",
       "      <td>0.129677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>286924</td>\n",
       "      <td>0.131667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>147742</td>\n",
       "      <td>0.133411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>166941</td>\n",
       "      <td>0.133801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>314843</td>\n",
       "      <td>0.136755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>120</th>\n",
       "      <td>112234</td>\n",
       "      <td>0.643628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>121</th>\n",
       "      <td>374420</td>\n",
       "      <td>0.656060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122</th>\n",
       "      <td>163039</td>\n",
       "      <td>0.657168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123</th>\n",
       "      <td>376488</td>\n",
       "      <td>0.660902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>124</th>\n",
       "      <td>340630</td>\n",
       "      <td>0.667528</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>125</th>\n",
       "      <td>406872</td>\n",
       "      <td>0.672001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>126</th>\n",
       "      <td>410626</td>\n",
       "      <td>0.685766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>127</th>\n",
       "      <td>175682</td>\n",
       "      <td>0.703779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>128</th>\n",
       "      <td>271135</td>\n",
       "      <td>0.744174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>129</th>\n",
       "      <td>321035</td>\n",
       "      <td>0.747395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>130</th>\n",
       "      <td>214088</td>\n",
       "      <td>0.747702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>131</th>\n",
       "      <td>185121</td>\n",
       "      <td>0.750349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>260364</td>\n",
       "      <td>0.765961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133</th>\n",
       "      <td>142676</td>\n",
       "      <td>0.798026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>275954</td>\n",
       "      <td>0.814028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>278344</td>\n",
       "      <td>0.815136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>168093</td>\n",
       "      <td>0.830194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>112414</td>\n",
       "      <td>0.842586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>218321</td>\n",
       "      <td>0.848125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>372942</td>\n",
       "      <td>0.851715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>407825</td>\n",
       "      <td>0.864701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>317809</td>\n",
       "      <td>0.875759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>443364</td>\n",
       "      <td>0.879390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>223571</td>\n",
       "      <td>0.880806</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>202697</td>\n",
       "      <td>0.884560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>233172</td>\n",
       "      <td>0.888868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>317323</td>\n",
       "      <td>0.892130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>271682</td>\n",
       "      <td>0.902019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>103570</td>\n",
       "      <td>0.905753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>412638</td>\n",
       "      <td>0.912912</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     SK_ID_CURR    TARGET\n",
       "0        102293  0.006073\n",
       "1        277290  0.007119\n",
       "2        108763  0.013971\n",
       "3        318857  0.016002\n",
       "4        324995  0.018854\n",
       "5        170203  0.022567\n",
       "6        214141  0.022608\n",
       "7        347097  0.025275\n",
       "8        430467  0.034897\n",
       "9        252092  0.037564\n",
       "10       268373  0.040969\n",
       "11       116228  0.041708\n",
       "12       192289  0.046139\n",
       "13       240888  0.050098\n",
       "14       337868  0.050509\n",
       "15       384657  0.061936\n",
       "16       435099  0.075538\n",
       "17       434608  0.092893\n",
       "18       405690  0.097079\n",
       "19       177820  0.097571\n",
       "20       401547  0.101448\n",
       "21       315494  0.106249\n",
       "22       334273  0.107029\n",
       "23       213935  0.118989\n",
       "24       409985  0.127790\n",
       "25       447361  0.129677\n",
       "26       286924  0.131667\n",
       "27       147742  0.133411\n",
       "28       166941  0.133801\n",
       "29       314843  0.136755\n",
       "..          ...       ...\n",
       "120      112234  0.643628\n",
       "121      374420  0.656060\n",
       "122      163039  0.657168\n",
       "123      376488  0.660902\n",
       "124      340630  0.667528\n",
       "125      406872  0.672001\n",
       "126      410626  0.685766\n",
       "127      175682  0.703779\n",
       "128      271135  0.744174\n",
       "129      321035  0.747395\n",
       "130      214088  0.747702\n",
       "131      185121  0.750349\n",
       "132      260364  0.765961\n",
       "133      142676  0.798026\n",
       "134      275954  0.814028\n",
       "135      278344  0.815136\n",
       "136      168093  0.830194\n",
       "137      112414  0.842586\n",
       "138      218321  0.848125\n",
       "139      372942  0.851715\n",
       "140      407825  0.864701\n",
       "141      317809  0.875759\n",
       "142      443364  0.879390\n",
       "143      223571  0.880806\n",
       "144      202697  0.884560\n",
       "145      233172  0.888868\n",
       "146      317323  0.892130\n",
       "147      271682  0.902019\n",
       "148      103570  0.905753\n",
       "149      412638  0.912912\n",
       "\n",
       "[150 rows x 2 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:33.901688Z",
     "start_time": "2018-08-22T09:28:33.893186Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SK_ID_CURR</th>\n",
       "      <th>TARGET</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>367663</td>\n",
       "      <td>0.914307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>122257</td>\n",
       "      <td>0.919149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>137590</td>\n",
       "      <td>0.923334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>447230</td>\n",
       "      <td>0.932320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>193198</td>\n",
       "      <td>0.948814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>219817</td>\n",
       "      <td>0.949573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>442195</td>\n",
       "      <td>0.952651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>163895</td>\n",
       "      <td>0.954087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>352451</td>\n",
       "      <td>0.956959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>311024</td>\n",
       "      <td>0.960918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>365575</td>\n",
       "      <td>0.985352</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    SK_ID_CURR    TARGET\n",
       "0       367663  0.914307\n",
       "1       122257  0.919149\n",
       "2       137590  0.923334\n",
       "3       447230  0.932320\n",
       "4       193198  0.948814\n",
       "5       219817  0.949573\n",
       "6       442195  0.952651\n",
       "7       163895  0.954087\n",
       "8       352451  0.956959\n",
       "9       311024  0.960918\n",
       "10      365575  0.985352"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:47.513545Z",
     "start_time": "2018-08-22T09:28:47.502390Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(48744, 2)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_new = pd.concat([sub_0, sub_1, sub_other])\n",
    "sub_new.sort_values('TARGET', inplace=True)\n",
    "sub_new.reset_index(drop=True, inplace=True)\n",
    "\n",
    "sub_new.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:48.960226Z",
     "start_time": "2018-08-22T09:28:48.956392Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(48744, 2)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:28:50.621106Z",
     "start_time": "2018-08-22T09:28:50.614954Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(48744,)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_new.SK_ID_CURR.unique().shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-22T09:29:08.408234Z",
     "start_time": "2018-08-22T09:29:07.989760Z"
    }
   },
   "outputs": [],
   "source": [
    "sub_new.to_csv('../output/stochastic_blending_v3-3_0.411rk7_0.304mw1_0.285t1_0.81031CV_0.3Adv_Leak002.csv.gz', index=False, compression='gzip')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
